const BaseUrl = "/api";

const json2url = (json) => {
    let arr = [];
    for (let name in json) {
        arr.push(name + "=" + json[name]);
    }
    return arr.join("&");
};
const http = ({ url, method = "get", params = {}, data }) => {
    const token = sessionStorage.getItem("token") || "";

    const option = {
        method,
        headers: {
            "Content-Type": "application/json",
            token: token,
        },
    };
    if (data) {
        option.body = JSON.stringify(data);
    }

    return fetch(BaseUrl + url + "?" + json2url(params), option).then(
        (response) => {
            return response.json();
        }
    );
};
export default http;
